//
//  ModifyPwdViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/11.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "ModifyPwdViewController.h"

@interface ModifyPwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTF;

@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *msgBtn;
/** 倒计时*/
@property (nonatomic, assign) NSInteger timeCount;

@end

@implementation ModifyPwdViewController
{
    NSTimer * _timer;
}

- (void)dealloc{
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    _timer = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    [_timer invalidate];
    [self resetVerifyStatus];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改登录密码";
    self.view.backgroundColor = kBgColor;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - btn clicked
- (IBAction)msgBtnAction:(UIButton *)sender {
    [self getCode];
    [self resetVerifyStatus];
    self.msgBtn.enabled = NO;
    // 定时器
    self.timeCount = 30;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    [_timer setFireDate:[NSDate distantPast]];
    NSLog(@"get code");
}

- (IBAction)confirmBtnAction:(UIButton *)sender {
    if (self.codeTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入验证码！"];
        return;
    }
    if (self.oldPwdTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入新密码！"];
        return;
    }
    if (self.pwdTF.text.length == 0) {
        [MBProgressHUD showError:@"请再次输入新密码！"];
        return;
    }
    if (![self.oldPwdTF.text isEqualToString:self.pwdTF.text]) {
        [MBProgressHUD showError:@"两次输入的密码不一致！"];
        return;
    }
    [self requestData];
}


#pragma mark - request data
- (void)getCode{
    [LBNetworkingInstance requestGET:getVerifyCode parameters:@{@"phoneNumber":[LBUserManage defaultManager].userModel.MOBILE?:@""} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
    }];
}

- (void)requestData{
    __weak typeof(self) weakSelf = self;
    [LBNetworkingInstance requestGET:modifyPassword parameters:@{@"memberId":[LBUserManage defaultManager].user_id?:@"",@"valiteCode":self.codeTF.text?:@"",@"updateInfo":self.pwdTF.text?:@"",@"type":@"2"} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        if (code == YCNetworkingStatusCodeSuccess){
            [LBUserManage defaultManager].userModel = [UserModel yy_modelWithJSON:responseObject[@"Data"]];
            [MBProgressHUD showSuccess:@"修改密码成功！"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            if (code == 998) {
                [MBProgressHUD showError:message];
            }
        }
    } failure:^(NSError *err) {
        
    }];
}

#pragma mark - 定时器
-(void)timer:(NSTimer *)timer
{
    self.timeCount --;
    if (self.timeCount > 0) {
        NSString * str = [NSString stringWithFormat:@"%lds",(long)self.timeCount];
        [self.msgBtn setTitle:str forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    }else{
        [self resetVerifyStatus];
    }
}

#pragma mark - 重设获取验证码状态
- (void)resetVerifyStatus{
    NSString * str = @"获取验证码";
    self.msgBtn.enabled = YES;
    [self.msgBtn setTitle:str forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:kMainColor forState:UIControlStateNormal];
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    [_timer invalidate];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

